package main

import "fmt"

func selectionSort(arr []int) []int {
	l := len(arr)
	if l == 0 {
		return arr
	}
	for i := 0; i < l; i++ {
		min := i
		for j := i + 1; j < l; j++ {
			if arr[j] < arr[min] {
				min = j
			}
		}
		arr[i], arr[min] = arr[min], arr[i]
	}
	return arr
}

func main() {
	fmt.Printf("selectionSort start\n")

	arrInit := []int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9, 5, 0, 2, 8, 8}
	fmt.Printf("arrInit:%v\n", arrInit)

	arrSorted := selectionSort(arrInit)
	fmt.Printf("arrSorted:%v\n", arrSorted)

	arrSorted = selectionSort1(arrInit)
	fmt.Printf("arrSorted:%v\n", arrSorted)
}

//@brief 尝试手工重新实现
func selectionSort1(arr []int) []int {
	if len(arr) <= 0 {
		return arr
	}

	for i := 0; i < len(arr); i++ {
		min := i
		for j := i + 1; j < len(arr); j++ {
			if arr[min] > arr[j] {
				min = j
			}
		}
		arr[i], arr[min] = arr[min], arr[i]
	}

	return arr
}
